<?php
/**
 * 21天减重测评算法
 * Created by PhpStorm.
 * User: liao
 * Date: 2016/12/12
 * Time: 16:41
 */

namespace App\Services\Result;

use App\Interf\Resultable;
use Monolog\Logger;
use TestingConfig;
use ParamConfig;
use App\Models\LogHelper;

class SleepingTestingNewResult extends ZAhealthVtwoResult implements Resultable
{
//    private $answers;

    private $EvaluationAnalysisObj;

    private $DietRecommendationObj;


    public function __construct($answersStr = null)
    {
        $this->answers = $this->convertAnswer($answersStr);
        $this->EvaluationAnalysisObj = $this->configAttrObj(1, '评估分析');
        $this->DietRecommendationObj = $this->configAttrObj(2, '饮食推荐');

    }

    public function result()
    {

        $attrs = [];

        array_push($attrs, $this->getEvaluationAnalysisObj());

        array_push($attrs, $this->getDietRecommendationObj());


        $data['attrs'] = $attrs;
        $data['chatparam'] = $this->getChatparam();
        //图类型常规图
        $data['chatType'] = $this->getChatType();

        return $data;

    }


    protected function getEvaluationAnalysisObj()
    {
        $note = $this->getNoteByTag('tag.65.testing.part1');
        $singelAns = $this->getAnswerObjByIndex($this->answers, 21);
        $replaceStr = round($singelAns['weight']);
        $note = str_replace('@21', $replaceStr, $note);
        $obj = $this->EvaluationAnalysisObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note);
        LogHelper::log($obj);
        return $obj;
    }


    protected function getDietRecommendationObj()
    {
        $note = ParamConfig::getValue('tag.65.testing.part2.default');
        $recipe['banner_title'] = $note;
        $recipe['banner_pic'] = "";
        $recipeData = $this->getRecipedetail();
        $recipe['recipe'] = $recipeData;
        $obj = $this->DietRecommendationObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $recipe);
        return $obj;
    }

    protected function getNoteByTag($tag)
    {
        $values = ParamConfig::getByTag($tag);

        foreach ($values as $v) {
            $tempKey = $v['key'];
            $replaceKey = $v['key'];
            $forSize = substr_count("$tempKey", "@");

            for ($k = 0; $k < $forSize; $k++) {
                if ((stripos($replaceKey, '@') !== false)) {
                    $i = stripos($replaceKey, '@');

                    if (is_numeric($replaceKey[$i + 1]) && is_numeric($replaceKey[$i + 2])) {
                        $searchStr = substr($replaceKey, $i, 3);
                        $index = substr($replaceKey, $i + 1, 2);
                    } else {
                        $searchStr = substr($replaceKey, $i, 2);
                        $index = substr($replaceKey, $i + 1, 1);
                    }


                    $singelAns = $this->getAnswerObjByIndex($this->answers, $index);
                    $replaceStr = round($singelAns['weight']);
                }
                $replaceKey = str_replace($searchStr, $replaceStr, $replaceKey);

            }

            $str = $replaceKey;

            if (eval("return $str;")) {
                return $v['value'];
            }
        }

        return "";
    }


    /**
     * 图标对应的BMI数据
     */
    protected function getChatparam()
    {
        $answer = $this->getAnswerObjByIndex($this->answers, 21);

        return $answer['weight'];
    }


    protected function compactAttrObj($id, $title, $note, $extra = '')
    {
        return [
            'attr_id' => $id,
            'title' => $title,
            'note' => $note,
            'extra' => $extra
        ];
    }


    /**将answer转换为数组
     * @param $answerStr
     * @return array
     */
    protected function convertAnswer($answerStr)
    {
        LogHelper::log('FINAL ANSWER STR============>');
        LogHelper::log($answerStr);
        $answers = [];
        $answerArr = explode('##', $answerStr);
        if (count($answerArr) > 0) {
            foreach ($answerArr as $answer) {
                $singleArr = explode(':', $answer);
                if (count($singleArr) > 0) {
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers, $obj);
                }
            }
        }
        return $answers;
    }

    /**返回食谱
     * @return array|string
     */
    protected function getRecipe()
    {
        $recipeList = ParamConfig::getByTag('tag.65.testing.recipe');
        $recipe = count($recipeList) ? $recipeList[mt_rand() % count($recipeList)]['value'] : null;

        return $recipe;
    }
}